import { baseUrl } from './config.js'

export function ajax(url, data = {}, method = 'GET', header = {
	'content-type': 'application/json;charset=utf-8'
}) 
{
	return new Promise((resolve, reject) => {
		uni.showLoading({
			mask: true
		});

		uni.request({
			url: baseUrl + url,
			data:data,
			header: header,
			method:method,
			success: res => {
				uni.hideLoading()
				uni.stopPullDownRefresh()
				if (res.statusCode === 200) {
					resolve(res.data);
					//token过期
				} else {
					if (res.data.resultMsg == '访问令牌过期或无效' || res.data.resultMsg == '缺少访问令牌') {
						uni.showToast({
							title: '您的登陆已过期',
							icon: 'none',
							duration: 1800
						})
						uni.clearStorageSync()
						setTimeout(function() {
							uni.reLaunch({
								url: ''
							})
						}, 1500)
						return
					}
					uni.showToast({
						title: res.data.resultMsg || '连接服务器失败,请稍后重试!',
						icon:"none",
						duration:1500
					})
					reject( res.data )
				}
			},
			fail: e => {
				uni.showToast({
					title:'连接失败,请检查网络后再试!',
					icon:'none',
					duration:1500
				})
				uni.hideLoading()
			}
		})
	}).catch((e) => {});
}
